<?php
namespace src;
use classlib\input;
use src\logic\Cadmin;
use classlib\core\lang;
use classlib\core as C;
use classlib\core\controller;
use src\logic\lupfile;
class Cmission extends controller {

	public function main() {
		$view   = C\view::inst('extjs');
		$userid = Cadmin::chkaccess($view);
		$dlang  = lang::inst('dd');
		$type   = array('1'=>'一次性任务', '2'=>'循环任务', '3'=>'每日任务');
		$view->assign('type', $type);
		$view->assign('status', $dlang->get('sh0006'));
		$view->assign_by_ref('title', Cadmin::$name);
		$view->display('live/activity/mission.ext.js');
	}

	public function dolist() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		list($start, $limit, $sort) = Cadmin::offset();
		$orm    = C\model::load('pro.sng.missions');
		$total  = $orm->get_total($where);
		$list   = array();
		if ($total) {
			$list = $orm->get_list($start, $limit, $where, '*', $sort);
			$orm= C\model::load('pro.inc.gift');
			foreach ($list as &$item) {
				$item['stime']  = date('Y-m-d H:i:s', $item['stime']);
				$item['badge']  = '';
				if ($item['badgeid'] > 0) {
					$img = C\model::load('pro.inc.thumb')->get_one($item['badgeid']);
					$item['badge']  = (empty($img) || empty($img['src']))? '':$img['src'];
				}
				if (!empty($item['giftids'])) {
					$config= array();
					$gifts = explode(',', $item['giftids']);
					foreach ($gifts as $giftstr) {
						if (!$giftstr || strpos($giftstr, '-') === false) {
							continue;
						}
						list($giftid, $nums) = explode('-', $giftstr);
						$gift     = $orm->get_one($giftid);
						if (!$gift) {
							continue;
						}
						$config[] = $gift['name'].' x '.$nums;
					}
					$item['gifts']= implode(',', $config);
				}
			}
		}
		$view->assign_by_ref('total', $total);
		$view->assign_by_ref('dataset', $list);
		$view->display();
	}

	public function dosave() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		$id    = (int)input::post('id');
		$data  = array();
		$data['title']   = input::post('title');
		$data['msg']     = input::post('msg');
		$data['jump']    = input::post('jump');
		$data['type']    = (int)input::post('type');
		$data['badgeid'] = (int)input::post('badgeid');
		$data['exp']     = (int)input::post('exp');
		$data['coin']    = (int)input::post('coin');
		$data['rank']    = (int)input::post('rank');
		$data['status']  = (int)input::post('status');
		//验证上传的图片资料信息
		if(isset($_FILES['Filedata']) && is_uploaded_file($_FILES['Filedata']['tmp_name'])) {
			$src = lupfile::transfer($view, 'task', 'Filedata');
			if ($src) {
				$data['icon'] = $src;
			}
		}
		$data['giftids'] = input::post('giftids');
		if ($data['exp'] < 0 || $data['coin'] < 0) {
			$view->assign_error('2', '奖励经验值/星币数值不能小于0');
		}
		$data['giftids'] = trim($data['giftids']);
		if ($data['giftids']) {
			$orm     = C\model::load('pro.inc.gift');
			$giftids = explode(',', $data['giftids']);
			$gifts   = array();
			foreach ($giftids as $giftstr) {
				//格式不匹配
				if (!$giftstr || strpos($giftstr, '-') == false) {
					continue;
				}
				list($giftid, $nums) = explode('-', $giftstr);
				$gift     = $orm->get_one($giftid);
				if ($nums > 0 && $gift) {
					$gifts[] = $giftstr;
				}
			}
			$data['giftids'] = implode(',', $gifts);
		}
		$data['stime']   = C\config::$nowtime;
		if ($id > 0) {
			C\model::load('pro.sng.missions')->save($id, $data);
		} else {
			C\model::load('pro.sng.missions')->new_one($data);
		}
		$view->display();
	}
}